package com.it.schoolbookshop_back.entities.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.it.schoolbookshop_back.utils.BooleanToIntegerDeserializer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 用户设置 DTO（数字格式）
 * 返回给前端的设置值使用数字格式（1表示true，0表示false）
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserSettingsNumericDTO {
    
    /**
     * 是否接收用户消息通知
     */
    @JsonProperty("notification_messages")
    @JsonDeserialize(using = BooleanToIntegerDeserializer.class)
    private Integer notificationMessages;
    
    /**
     * 是否接收收藏通知
     */
    @JsonProperty("notification_favorites")
    @JsonDeserialize(using = BooleanToIntegerDeserializer.class)
    private Integer notificationFavorites;
    
    /**
     * 是否接收交易通知
     */
    @JsonProperty("notification_transactions")
    @JsonDeserialize(using = BooleanToIntegerDeserializer.class)
    private Integer notificationTransactions;
    
    /**
     * 从布尔型DTO转换为数字型DTO
     * 
     * @param booleanDTO 布尔型DTO
     * @return 数字型DTO
     */
    public static UserSettingsNumericDTO fromBooleanDTO(UserSettingsDTO booleanDTO) {
        if (booleanDTO == null) {
            return null;
        }
        
        UserSettingsNumericDTO numericDTO = new UserSettingsNumericDTO();
        numericDTO.setNotificationMessages(booleanDTO.getNotificationMessages() != null && booleanDTO.getNotificationMessages() ? 1 : 0);
        numericDTO.setNotificationFavorites(booleanDTO.getNotificationFavorites() != null && booleanDTO.getNotificationFavorites() ? 1 : 0);
        numericDTO.setNotificationTransactions(booleanDTO.getNotificationTransactions() != null && booleanDTO.getNotificationTransactions() ? 1 : 0);
        
        return numericDTO;
    }
    
    /**
     * 转换为布尔型DTO
     * 
     * @return 布尔型DTO
     */
    public UserSettingsDTO toBooleanDTO() {
        UserSettingsDTO booleanDTO = new UserSettingsDTO();
        booleanDTO.setNotificationMessages(notificationMessages != null && notificationMessages == 1);
        booleanDTO.setNotificationFavorites(notificationFavorites != null && notificationFavorites == 1);
        booleanDTO.setNotificationTransactions(notificationTransactions != null && notificationTransactions == 1);
        
        return booleanDTO;
    }
} 